package com.csr;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.SparseArray;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ExpandableListView;
import android.widget.Spinner;
import android.widget.TextView;

public class WeeklyStatementActivity extends Activity
{
	ProgressDialog pd=null;
	Handler handler=new Handler();
	MD5Generator md5Generator=new MD5Generator();
	HttpConn httpConn=new HttpConn();
	WeeklyStmtInfo weeklyStmtInfo=new WeeklyStmtInfo();
	Spinner spinnerGrpBy;
	ExpandableListView list_Statement;
	ArrayList<String> GroupbyName,TotalRequest,FaceValue,NetValue,ProviderTopupValue,ProviderNetValue,NetProfit;
	ExpandableListAdapter adapter;
	private static boolean getByDate,getByServices;
	TextView tv_notransaction;
	ArrayList<String> listItem;
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.weekly_stmt);
		list_Statement=(ExpandableListView)findViewById(R.id.list_weeklyStmt);
		spinnerGrpBy=(Spinner)findViewById(R.id.spinnerGropuBy);
		tv_notransaction=(TextView)findViewById(R.id.tv_notransaction);
        spinnerGrpBy.setOnItemSelectedListener(new OnItemSelectedListener()
        {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3)
			{
				
				Thread thread;
				switch (position)
				{
					case 0:
						getByDate=true;
						getByServices=false;
						
						pd=ProgressDialog.show(WeeklyStatementActivity.this,"please wait.",null);
				        pd.setContentView(R.layout.progress);
				       
				         thread=new Thread(getWeeklyStatement);
				        thread.start();
				        
						break;
					case 1:
						getByServices=true;
						getByDate=false;
						
						pd=ProgressDialog.show(WeeklyStatementActivity.this,"please wait.",null);
				        pd.setContentView(R.layout.progress);
				       
				        thread=new Thread(getWeeklyStatement);
				        thread.start();
						break;
				}
				
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
        	
		});		
        
        
        
	}	
	
	Runnable getWeeklyStatement=new Runnable()
	{
		
		ArrayList<WeeklyStmtInfo> stmtArray=new ArrayList<WeeklyStmtInfo>();
		public void run() 
		{
		
			try
			{
				String checksum;
				String URL = null;
				
				if(getByDate)
				{
				
				checksum=md5Generator.MD5(md5Generator.SHA1(AppData.getUserInfo().getLoginID()+"|"+AppData.agentInfo.getPhone1()+"|"+"|"+"|"+"|"+
								AppData.getUserInfo().getpublickey()));
				URL=AppData.hostURL+"/WebService/iTopUp/csrapp.php/TransactionSummary/LoginId/"+AppData.getUserInfo().getLoginID()
				 		   +"/Phone1/"+AppData.agentInfo.getPhone1()
						   +"/StartTime/"
						   +"/EndTime/"
						   +"/Groupby/"
						   +"/Checksum/"+checksum;
				}
				else if(getByServices)
				{
					checksum=md5Generator.MD5(md5Generator.SHA1(AppData.getUserInfo().getLoginID()+"|"+AppData.agentInfo.getPhone1()+"|"+"|"+"|service"+"|"+
							AppData.getUserInfo().getpublickey()));
					URL=AppData.hostURL+"/WebService/iTopUp/csrapp.php/TransactionSummary/LoginId/"+AppData.getUserInfo().getLoginID()
			 		   +"/Phone1/"+AppData.agentInfo.getPhone1()
					   +"/StartTime/"
					   +"/EndTime/"
					   +"/Groupby/service"
					   +"/Checksum/"+checksum;
				}
				 System.out.println("URL===>\n"+URL);
				 String response=httpConn.createHttpConn(URL);
				 System.out.println("Response:"+response);
				 
				 final JSONObject jsonObject=new JSONObject(response);
				 JSONArray jsonArray;
				 if(jsonObject.getString("ResponseCode").equals("000"))
	             {
					 GroupbyName=new ArrayList<String>();
					 TotalRequest=new ArrayList<String>();
					 FaceValue=new ArrayList<String>();
					 NetValue=new ArrayList<String>();
					 ProviderTopupValue=new ArrayList<String>();
					 ProviderNetValue=new ArrayList<String>();
					 NetProfit=new ArrayList<String>();
					 
					 if(jsonObject.has("GroupbyName"))
					 {
						     jsonArray=jsonObject.getJSONArray("GroupbyName");
							  System.out.println("Length of GroupbyName:"+jsonArray.length()+
	                                  "\ngetting GroupbyName");
							  for(int i=0;i<jsonArray.length();i++)
				                {
								  GroupbyName.add(jsonArray.getString(i));
				                }
					}
					if(jsonObject.has("TotalRequest"))
		            {	
		                jsonArray=jsonObject.getJSONArray("TotalRequest");
		                System.out.println("Length of TotalRequest:"+jsonArray.length()+
		                                    "\ngetting TotalRequest");
		                for(int i=0;i<jsonArray.length();i++)
		                {
		                	TotalRequest.add(jsonArray.getString(i));
		                }
		            }
					
					if(jsonObject.has("FaceValue"))
		            {	
		                jsonArray=jsonObject.getJSONArray("FaceValue");
		                System.out.println("Length of FaceValue:"+jsonArray.length()+
		                                    "\ngetting FaceValue");
		                for(int i=0;i<jsonArray.length();i++)
		                {
		                	FaceValue.add(jsonArray.getString(i));
		                }
		            }
					if(jsonObject.has("NetValue"))
		            {	
		                jsonArray=jsonObject.getJSONArray("NetValue");
		                System.out.println("Length of NetValue:"+jsonArray.length()+
		                                    "\ngetting NetValue");
		                for(int i=0;i<jsonArray.length();i++)
		                {
		                	NetValue.add(jsonArray.getString(i));
		                }
		            }
					if(jsonObject.has("ProviderTopupValue"))
		            {	
		                jsonArray=jsonObject.getJSONArray("ProviderTopupValue");
		                System.out.println("Length of ProviderTopupValue:"+jsonArray.length()+
		                                    "\ngetting ProviderTopupValue");
		                for(int i=0;i<jsonArray.length();i++)
		                {
		                	ProviderTopupValue.add(jsonArray.getString(i));
		                }
		            }
		            if(jsonObject.has("ProviderNetValue"))
		            {	
		                jsonArray=jsonObject.getJSONArray("ProviderNetValue");
		                System.out.println("Length of ProviderNetValue:"+jsonArray.length()+
		                                    "\ngetting ProviderNetValue");
		                for(int i=0;i<jsonArray.length();i++)
		                {
		                	ProviderNetValue.add(jsonArray.getString(i));
		                }
		            } 
		            if(jsonObject.has("NetProfit"))
		            {	
		                jsonArray=jsonObject.getJSONArray("NetProfit");
		                System.out.println("Length of NetProfit:"+jsonArray.length()+
		                                    "\ngetting NetProfit");
		                for(int i=0;i<jsonArray.length();i++)
		                {
		                	NetProfit.add(jsonArray.getString(i));
		                }
		            } 
		            
		            System.out.println("=================>Printing Statement");
		             System.out.println("Size:"+GroupbyName.size());
		             for(int i=0;i<GroupbyName.size();i++)
		             {
		                  System.out.println("GroupbyName:"+GroupbyName.get(i));
		                  System.out.println("TotalRequest:"+TotalRequest.get(i));
		                  System.out.println("FaceValue:"+FaceValue.get(i));
		                  System.out.println("NetValue:"+NetValue.get(i));
		                  System.out.println("ProviderTopupValue :"+ProviderTopupValue.get(i));
		                  System.out.println("ProviderNetValue:"+ProviderNetValue.get(i));
		                  System.out.println("NetProfit:"+NetProfit.get(i)); 
		                  System.out.println("=====================");
		             }
		             
		              
		               if(stmtArray!=null)stmtArray.clear();
		              
		              
			           for(int i=0;i<GroupbyName.size();i++)
			           {
			        	   
			        	   WeeklyStmtInfo ws=new WeeklyStmtInfo();
			        	   
			               ws.setGroupbyName((String)GroupbyName.get(i));
			               ws.setTotalRequest((String)TotalRequest.get(i));
			               ws.setFaceValue((String)FaceValue.get(i));
			               ws.setNetValue((String)NetValue.get(i));
			               ws.setProviderTopupValue((String)ProviderTopupValue.get(i)); 
			               ws.setProviderNetValue((String)ProviderNetValue.get(i));
			               ws.setNetProfit((String)NetProfit.get(i));
			             
			               stmtArray.add(ws);
			           } 
		              
	             }
				 listItem =new ArrayList<String>();
				 for(int i=0;i<stmtArray.size();i++)
				 {
					 listItem.add(stmtArray.get(i).getGroupbyName());
				 }
				 
				 System.out.println("List Item Size:"+listItem.size());
				// adapter=new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, listItem);
				 adapter = new ExpandableListAdapter(WeeklyStatementActivity.this, listItem, stmtArray);
				 
				 runOnUiThread(addToListView);
				 pd.dismiss();
			}//try
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
	}; 
	Runnable addToListView=new Runnable() 
	{
		public void run() 
		{
			
			 list_Statement.setAdapter(adapter);
			 if(!(listItem.size()>0))
			 {
				 tv_notransaction.setVisibility(TextView.VISIBLE);
			 }
			 
		}
	};
}